// pages/userCenter/signorderDetail/signorderDetail.js
var app = getApp();
var util = require("../../../utils/util.js");
var config = require("../../../utils/config.js");
var WxParse = require("../../../wxParse/wxParse.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screen: app.data.screen,
    hidden: 2,
    message: {},
    id: '',
  },
  handelDownload() {
    for (var i = 0; i < this.data.message.downurl.length; i++) {
      wx.downloadFile({
        url: this.data.message.downurl[i], 
        success(res) {
          // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
          console.log(res)
          if (res.statusCode === 200) {
            wx.openDocument({
              filePath: res.tempFilePath,
              success: function (res) {
                // util.showModal('温馨提示', '下载成功', '', '')
              }
            })
          }
        }
      })
    }
  },
  // 重新提交
  submitTo(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/signUp/signUp?enroll_id=' + e.currentTarget.dataset.enroll_id + '&id=' + e.currentTarget.dataset.id,
    })
  },
  waiton() {
    this.setData({
      isDisabled: true
    })
    setTimeout(() => {
      this.setData({
        isDisabled: false
      })
    }, 2000);
  },
  signMoney(e) {
    this.waiton();
    var formid = e.detail.formId;
    util.getOpenID((e) => {
      var parameters = {
        openid: e.openid,
        pro_id: this.data.message.pro_id,
        enroll_id: this.data.message.enroll_id,
        sign_money: this.data.message.sign_money
      };
      var parameters1 = {
        openid: e.openid,
        formid: formid,
      }
      // 收集模板信息
      util.collectRequest(config.collectUser, parameters1, (res) => {

      });
      util.postRequest(config.wxpay, parameters, function (s) {
        console.log('调起支付');
        console.log(s);
        if (s.code == 200) {
          wx.requestPayment({
            'timeStamp': s.data.timeStamp,
            'nonceStr': s.data.nonceStr,
            'package': s.data.package,
            'signType': 'MD5',
            'paySign': s.data.paySign,
            'success': function (res) {            
              wx.navigateTo({
                url: "/pages/userCenter/bidOrder/bidOrder?toubiao='0'"
              })
            },
            'fail': function (res) {
              console.log(res);
            },
            'complete': function (res) {
              
            }
          });
        } else {
          util.showModal('温馨提示', '系统错误', '', '')
        }
      }, function (fail) { }, function (complete) { }, true);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          that.request();

        } else {
          wx.navigateTo({
            url: '/pages/login/login',
          });
        }
      }
    })
    if (that.data.message.length <= 0) {
      that.request();
    }
  },

  //  网络请求
  request() {
    util.getOpenID((e) => {
      var parameters = {
        openid: e.openid,
        id: this.data.id
      }
      util.postRequest(config.signDetail, parameters, (res) => {
        console.log(res)
        if (res.code == 200) {
          this.setData({
            hidden: 1,
            message: res.data,
          })
        } else if (res.code == 201) {
          wx.showToast({
            title: '请求失败!',
            icon: 'none'
          })
        }
      }, () => {
        //请求失败
      }, () => {
        //请求完成
      }, true);
    }, true);
  },
  // 点击预览
  previewImg(e) {
    var index = e.currentTarget.dataset.index;
    var imgArr = [];
    imgArr.push(this.data.imgurl[index]);
    wx.previewImage({
      current: this.data.imgurl[index],     //当前图片地址
      urls: imgArr,               //所有要预览的图片的地址集合 数组形式
      success(res) {

      },
      fail(res) { },
      complete(res) { },
    })
  },

})